package com.ex

import com.android.build.gradle.AppExtension
import com.android.build.gradle.AppPlugin
import com.android.build.gradle.internal.api.ApplicationVariantImpl
import com.ex.util.Reflect
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.Task.TASK_GROUP

class ThinRPlugin : Plugin<Project> {

    override fun apply(project: Project) {

        print("apply plugin start")


        if (isAppPlugin(project)) {
            val android = project.extensions.findByType(AppExtension::class.java) as AppExtension

            android.registerTransform(ThinRTransform(project))

            project.afterEvaluate {

                val app = project.plugins.findPlugin(AppPlugin::class.java)


//                val reflect = Reflect.on(app!!.variantManager)

                val taskArgs = hashMapOf<String, String>()
                taskArgs[TASK_GROUP] = "neonGroup"



                android.applicationVariants.forEach { it ->

                    val appVariant = it as ApplicationVariantImpl
                    appVariant.variantData.getTaskName("assemble", "Debug")

                    println("-------------debug thinR plugin-------------------------------->")
                }


            }
        }
    }


    fun isAppPlugin(project: Project): Boolean {
        return project.plugins.findPlugin(AppPlugin::class.java) != null
    }
}